package cn.yaboya.basic.controller;

import cn.yaboya.basic.exception.BusinessException;
import cn.yaboya.basic.service.ILoginService;
import cn.yaboya.basic.utils.AjaxResult;
import cn.yaboya.basic.vo.LoginVo;
import cn.yaboya.basic.vo.WechatVo;
import cn.yaboya.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginVo vo){
        try {
            Map<String, Object> map = loginService.account(vo);
            return new AjaxResult().me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new  AjaxResult().me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }


    }

    @PostMapping("/phone")
    public AjaxResult phone(@RequestBody LoginVo vo){
        try {
            Map<String, Object> map = loginService.phone(vo);
            return new AjaxResult().me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new  AjaxResult().me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatVo vo){
        try {
            return loginService.wechat(vo);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new  AjaxResult().me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatVo vo){
        try {
            HashMap<String, Object> map = loginService.binder(vo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new  AjaxResult().me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}
